package com.qf.controller;

import com.qf.pojo.Goods;
import com.qf.service.GoodsService;
import com.qf.util.QNutil;
import com.qf.util.ResultMsg;
import com.qf.vo.VoGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("/search")
    public ResultMsg search(@DateTimeFormat(pattern="yyyy-MM-dd") Date from,@DateTimeFormat(pattern="yyyy-MM-dd") Date to){
        return goodsService.selectAllBySearch(from,to);
//        return null;
    }

//    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @PostMapping("/upload")
    public ResultMsg upload(MultipartFile file) {//page当前页码；limit每页显示的行数
        //调用七牛云进行图片的上传处理，接收一个存储地址

        ResultMsg resultMsg= goodsService.upload(file);//limit,page
        return resultMsg;
    }
    @GetMapping("/all2")
    public ResultMsg all2(HttpServletRequest request,Goods goods) {//page当前页码；limit每页显示的行数
        String time=request.getParameter("time");// closetime="19:20:30"   http://localhost:8808/closetime=19:20:30

       // ResultMsg resultMsg= goodsService.selectAll(page,limit);//limit,page
        return null;
    }
//    @RequestMapping("/all")
    @GetMapping("/all")
    public ResultMsg all(int limit,int page) {//page当前页码；limit每页显示的行数
        ResultMsg resultMsg= goodsService.selectAll(page,limit);//limit,page
        return resultMsg;
    }

    @RequestMapping("/del/{id}")
    public ResultMsg del(@PathVariable("id") int gid) {
        //删除操作
        return goodsService.deleteById(gid);

    }

    @RequestMapping("/one/{id}")
    public ResultMsg one(@PathVariable("id") int gid) {

        return goodsService.selectOne(gid);//Goods--id/name/price...typeid/type

    }

    @RequestMapping("/update")
    public ResultMsg update(VoGoods goods) throws ParseException {

        System.out.println(goods);
//        System.out.println("gid = " + gid);
//        System.out.println("price = " + price);
//        System.out.println("pubdate = " + pubdate);
//        System.out.println("num = " + num);
//        Goods goods=new Goods();
//        goods.setPubdate(new SimpleDateFormat("yyyy-MM-dd").parse(pubdate));
//        goods.setGid(gid);
//        goods.setGname(gname);
//        goods.setNum(num);
//        goods.setPrice(price);
        return goodsService.update(goods);
//        return null;
    }

}
